<?php
class PublicAction extends Action
{
	public function error404()
	{
		header("HTTP/1.0 404 Not Found");
		$this->display('Public/404');
	}

	public function adminLogin(){
		if (D('Admin')->islogin()){
			redirect(U('admin/index'));
		}
		$this->display();
	}

	public function doAdminLogin(){
		if (!$_POST)
			$this->error('非法提交');
		if (md5($_POST['verify'])!= $_SESSION['verify']){
			$this->error("验证码错误!");
		}
		$admin = D('Admin');
		if ($admin->create()){
			if ($admin->checkLogin($_POST['username'],$_POST['password'])){
				redirect(U('admin/index'));
			}else {
				$this->error("用户密码错误!");
			}

		}else {

			$this->error($admin->getError());
		}
	}
	/**
    +----------------------------------------------------------
    *  验证码
    +----------------------------------------------------------
    */
	public function verify(){
		import("ORG.Util.Image");
		Image::buildImageVerify();
	}
	/**
    +----------------------------------------------------------
    * 管理员退出登录
    +----------------------------------------------------------
    */
	public function adminLoginOut(){
		unset($_SESSION['YunPHPAdmin']);
		unset($_SESSION['YunPHPName']);
		redirect(__ROOT__.'/index.php');
	}
	
	public function updateSiteConfig()
	{
		if (!$_POST)
			$this->error('非法提交');
		$setting = $_POST['setting'];
		foreach ($setting as $key => $value)
		{
			D('setting')->updateConfig($key,$value);
		}
		$newconfig = M('setting')->select();
		foreach ($result as $v)
		{
			$site_config[$v['skey']] = $v['svalue'];			
		}
		F('site_config',$site_config);//写入配置缓存
		$this->success('修改系统配置成功!');
		
	}
	
	public function editAdminPwd()
	{
		if (!$_POST)
			$this->error('非法提交');
		$adminid = $_SESSION['YunPHPAdmin'];
		$oldpwd = $_POST['oldpw'];
		$r_tmp = M('admin')->where(array('id'=>$adminid))->find();
		
		if (md5($oldpwd)!=$r_tmp['password'])
		{
			$this->error('原密码错误,请重新输入!');
		}
		else
		{
			M('admin')->data(array('password'=>md5($_POST['newpw'])))->where(array('id'=>$adminid))->save();
			D('admin')->checkLogin($_SESSION['YunPHPName'],$_POST['newpw']);
			$this->success('修改创始人密码成功!');
		}
	}
	
	/**
	 * 
	 * 添加分类 addCategory
	 */
	public function addCategory()
	{
		/*dump($_POST);
		exit();*/
		if (!$_POST)
			$this->error('非法提交');
		$category = D('category');
		if ($category->create())
		{
			if ($category->data($_POST)->add())
			{
				$category->updateCategoryCache();//更新栏目缓存
				$this->success('添加分类成功!');
			}
			else
			{
				$this->error('添加分类失败!');
			}
		}
		else
		{
			$this->error($category->getError());
		}
	}
	
	//编辑分类
	public function editCategory()
	{
		if (!$_POST)
			$this->error('非法提交');
		
		$category = D('category');
		if ($category->create())
		{
			if ($category->data($_POST)->save())
			{
				$category->updateCategoryCache();//更新栏目缓存
				$this->success('编辑分类成功!');
			}
			else
			{
				$this->error('编辑分类失败!');
			}
		}
		else
		{
			$this->error($category->getError());
		}
	}
	
	//添加文章
	public function addArticle()
	{
		if (!$_POST)
			$this->error('非法提交');
		$posts = D('posts');
		if ($posts->create())
		{
			if (MAGIC_QUOTES_GPC)//如果开启自动转义
			{
				$_POST['content'] = stripslashes($_POST['content']);
			}
			if (empty($_POST['keywords']))
			{
//		做自动提取关键词		
			}
			if (empty($_POST['description']))
			{
				//自动提取描述
				$_POST['description'] = cn_substr_utf8(SpHtml2Text($_POST['content']),240);
				$_POST['description'] = trim(preg_replace('/#p#|#e#/','',$_POST['description']));
				
			}
			$_POST['createtime'] = time();
			$_POST['createdate'] = date('Y-m-d',time());
			
			if ($insertId = $posts->data($_POST)->add())
			{
				D('tag')->addTag($_POST['tagname'],$insertId);//插入tag标签
				$this->success('添加文章成功!');
			}
			else
			
			{
//				dump($_POST);
				$this->error('添加失败!');
			}
		}
		else
		{
			$this->error($posts->getError());
		}	
	}
	
	//编辑文章
	public function editArticle()
	{
		if (!$_POST)
			$this->error('非法提交!');
		$posts = D('posts');
		if ($posts->create())
		{
			$tagStr = $_POST['tagname'];
			unset($_POST['tagname']);
			if (MAGIC_QUOTES_GPC)//如果开启自动转义
			{
				$_POST['content'] = stripslashes($_POST['content']);				
			}
			/*dump($_POST);
			dump($tagStr);
			exit();*/
			if ($posts->data($_POST)->where(array(id=>$_POST['id']))->save())
			{
//				D('tag')->updateTag($tagStr,$_POST['id']);
				$this->success('编辑文章成功!');
			}
			else
			{
				
				$this->error('编辑文章失败!');
			}
		}
		else
		{
			$this->error($posts->getError());
		}
	}
	
	//添加评论
	public function addComment()
	{
		if (!$_POST)
			$this->error('非法提交!');
		$comment = D('comment');
		if ($comment->create())
		{
			Load('extend');
			$_POST['comment_author_ip'] = get_client_ip();
			$_POST['comment_date'] = date("Y-m-d H:i:s",time());
			if ($comment->data($_POST)->add())
			{
				$this->success('评论成功,等待管理员审核!');
			}
			$this->error('评论失败!');
			
		}
		else
		{
			$this->error($comment->getError());
		}
		
	}
	
	public function right()
	{
		$rightData['hotarticle'] = self::getArticleByClick();
		$rightData['hottag'] = self::getTagByLength();
		$rightData['articlebycomment'] = self::getArticleByComment();
		$this->assign('rightData',$rightData);
		
	}
	
	
	//获取热门文章
	private static function getArticleByClick($num = '10')
	{
		if (!F('getArticleByClick'))
		{
			$tempdata = M('posts')->order('click_count desc')->field('subject,id,click_count')->limit('0,'.$num)->select();
			F('getArticleByClick',$tempdata);
		}
		return F('getArticleByClick');
	}
	
	//获取最新评论的文章
	private static function getArticleByComment($num = '10')
	{
		if (!F('getArticleByComment'))
		{
			$comment = D('comment');
			$array_post_ID = $comment->getArrayPostID($num);
			$ArticleByComment = array();
			foreach ($array_post_ID as $aid)
			{
				$ArticleByComment[]=M('posts')->order('id desc')->where(array('id'=>$aid['comment_post_ID']))->field('subject,id')->find();
			}
			F('getArticleByComment',$ArticleByComment);
		}
		
		return F('getArticleByComment');
		
	}
	//获取热门TAG
	private static function getTagByLength()
	{
		if (!F('getTagByLength'))
		{
			$tempdata = M('tag')->order('LENGTH(aid) desc')->field('tagname')->limit('0,24')->select();
			F('getTagByLength',$tempdata);
		}
		return F('getTagByLength');
	}

}
?>